import pygame
from bullet import Bullet
from bloodBar import BloodBaar
from settings import *

class Hero(pygame.sprite.Sprite):
    def __init__(self, screen, screen_rect):


        pygame.sprite.Sprite.__init__(self)

        self.screen = screen
        
        self.image = pygame.image.load('images/rocketet.png')
        self.image = pygame.transform.scale(self.image, (60, 60))
        
        self.rect = self.image.get_rect()
        

        self.rect.centerx = screen_rect.centerx
        self.rect.y = screen_rect.height - self.rect.height - 20
        self.screen_rect = screen_rect
        self.blood = 10
        self.alive = True

        self.speed = 5

        # 子弹集合
        self.bullets = pygame.sprite.Group()
        self.bCount = 0
        self.dead_action = 0

        # 血条
        self.bBar = BloodBaar(screen, screen_rect)
    

    def display(self):
        self.screen.blit(self.image, self.rect)



    def connect(self, enemys):
        self.enemys = enemys

    def update(self):
        if self.alive == False:
            if self.dead_action > REFRESH_FPS:
                self.kill()
                print('Game Over')
                return
            self.displayDead()
            self.dead_action += 1
            return
        
        # 移动
        keys_press = pygame.key.get_pressed()
        if keys_press[pygame.K_UP]:
            self.rect.y -= 1 * self.speed
        if keys_press[pygame.K_DOWN]:
            self.rect.y += 1 * self.speed
        if keys_press[pygame.K_LEFT]:
            self.rect.x -= 1 * self.speed
        if keys_press[pygame.K_RIGHT]:
            self.rect.x += 1 * self.speed
        
        if self.rect.x <= 0:
            self.rect.x = 0
        if self.rect.x >= self.screen_rect.width - self.rect.width:
            self.rect.x = self.screen_rect.width - self.rect.width
        if self.rect.y <= 0:
            self.rect.y = 0
        if self.rect.y >= self.screen_rect.height - self.rect.height:
            self.rect.y = self.screen_rect.height - self.rect.height
        
        self.display()

        # 创建子弹
        self.bCount += 1
        if self.bCount == REFRESH_FPS / 4:
            self.bullets.add(Bullet('images/hero_bullet1.png', 2, 2, -10,45,20,self.screen, self))
            self.bCount = 0
        
        # 更新子弹
        for bul in self.bullets:
            bul.update()

        # 更新血条
        self.bBar.update(self.blood / 10)

        
        if self.blood <= 0:
            self.alive = False

            
        # 碰撞检测
        for em in self.enemys:
            # 撞机了
            if pygame.sprite.collide_rect(self, em) and em.blood > 0:
                self.alive = False
                self.bBar.update(0)
                em.blood = 0
                return
            # 打中敌机了
            for bul in self.bullets:
                if pygame.sprite.collide_rect(bul, em) and em.blood > 0:
                    em.blood -= bul.strength
                    bul.kill()
                for bul_en in em.bullets:
                    if pygame.sprite.collide_rect(bul, bul_en):
                        bul_en.blood -= bul.strength
                        bul.blood -= bul_en.strength 

            # 被敌机子弹打中了
            for bul in em.bullets:
                if pygame.sprite.collide_rect(bul, self):
                    self.blood -= bul.strength
                    self.bBar.update(self.blood / 10)
                    bul.kill()

    
    def displayDead(self):
        if self.dead_action == 0:
            self.image = pygame.image.load('images/hero_dead.png')
            self.image = pygame.transform.rotozoom(self.image, 0, 0.35)
        elif self.dead_action == REFRESH_FPS / 3:
            self.image = pygame.transform.rotozoom(self.image, 0, 0.56)
        elif self.dead_action == REFRESH_FPS / 1.5:
            self.image = pygame.transform.rotozoom(self.image, 0, 0.80)
        
        self.display()